package com.travel.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travel.common.UserContext;
import com.travel.entity.po.Remind;
import com.travel.entity.vo.RemindVo;
import com.travel.mapper.RemindMapper;
import com.travel.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户提醒控制器
 * 处理用户提醒相关的请求
 */
@RestController
@RequestMapping("/user/remind")
@Api(tags = "用户提醒")
@Slf4j
public class UserRemindController {

    @Autowired
    private RemindMapper remindMapper;

    /**
     * 根据日期查询用户提醒
     * 如果不指定日期，则返回当天的提醒
     * 
     * @param date 查询日期，格式为yyyy-MM-dd
     * @return 用户提醒列表
     */
    @GetMapping("/list")
    @ApiOperation("根据日期查询用户提醒")
    public Result<List<RemindVo>> getRemindsByDate(
            @ApiParam(value = "查询日期，格式为yyyy-MM-dd，不传则查询当天", example = "2025-04-20")
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        
        // 从用户上下文中获取用户ID
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.fail(401, "用户未登录");
        }
        
        // 如果日期为空，则使用当前日期
        if (date == null) {
            date = LocalDate.now();
        }
        
        log.info("查询用户[{}]在[{}]的提醒", userId, date);
        
        // 构建查询条件
        LambdaQueryWrapper<Remind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Remind::getUserId, userId.intValue())
                   .eq(Remind::getTime, date)
                   .orderByDesc(Remind::getId);
        
        // 查询提醒
        List<Remind> reminds = remindMapper.selectList(queryWrapper);
        
        // 转换为VO对象
        List<RemindVo> remindVos = reminds.stream().map(remind -> {
            RemindVo vo = new RemindVo();
            BeanUtils.copyProperties(remind, vo);
            return vo;
        }).collect(Collectors.toList());
        
        log.info("用户[{}]在[{}]有{}条提醒", userId, date, remindVos.size());
        
        return Result.success(remindVos);
    }
} 